﻿// D5XMessageBox.cpp: 实现文件
//

#include "stdafx.h"
#include "D5XMessageBox.h"
#include "afxdialogex.h"
#include "../FilterSDK/ia.h"
#include "resource.h"

constexpr int MAX_MSG_QUEUE_SIZE = 10;
// D5XMessageBox 对话框

IMPLEMENT_DYNAMIC(D5XMessageBox, CDialogEx)

D5XMessageBox::D5XMessageBox(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_MESSAGEBOX, pParent)
{

}

D5XMessageBox::~D5XMessageBox()
{
}

void D5XMessageBox::SetContent(CString content)
{
	m_pContent.SetWindowTextA(content);
}

void D5XMessageBox::SetTitle(CString title)
{
	SetWindowText(title);
}

void D5XMessageBox::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_STATIC_CRASH_TXT, m_pContent);
	DDX_Control(pDX, IDC_STATIC_ICON, m_pIcon);
}


BEGIN_MESSAGE_MAP(D5XMessageBox, CDialogEx)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTON1, &D5XMessageBox::OnBnClickedButton1)
	ON_WM_PAINT()
END_MESSAGE_MAP()


// D5XMessageBox 消息处理程序


void D5XMessageBox::OnClose()
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	ConfirmOrCloseShowMessage();
	//CDialogEx::OnClose();
}


void D5XMessageBox::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	ConfirmOrCloseShowMessage();
}


BOOL D5XMessageBox::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	HICON m_hIcon;
	m_hIcon = AfxGetApp()->LoadIcon(IDI_CYG_LOGO);
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	this->SetBackgroundColor(RGB(255, 255, 255));

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void D5XMessageBox::OnPaint()
{
	CPaintDC dc(this); // device context for painting
					   // TODO: 在此处添加消息处理程序代码
					   // 不为绘图消息调用 CDialogEx::OnPaint()

	CImage* img = new CImage;
	CRect rect;
	dc.SetStretchBltMode(HALFTONE);
	HRESULT result = img->Load("res/warning1.png");
	m_pIcon.GetWindowRect(&rect);
	ScreenToClient(&rect);
	if (SUCCEEDED(result)) {
		img->Draw(dc.m_hDC, rect);
	}
	else {
		IA_ERROR("Icon: warning.png丢失");
	}
	img->Destroy();
	IA_DELETE(img);
}

int D5XMessageBox::AddNewMessage(std::string title, std::string content)
{
	_pushMsgSync.lock();
	_msgQueue.push(std::pair<std::string, std::string>(title, content));
	if (_msgQueue.size() > MAX_MSG_QUEUE_SIZE) {
		_msgQueue.pop();
	}
	_pushMsgSync.unlock();
	ShowMessage(title, content);

	IA_RETURN_OK
}

void D5XMessageBox::ShowMessage(std::string title, std::string content)
{
	if (title.empty()) {
		title = "消息提示";
	}
	SetTitle(title.c_str());
	SetContent(content.c_str());
	ShowWindow(SW_SHOW);
}

void D5XMessageBox::ConfirmOrCloseShowMessage()
{
	ShowWindow(SW_HIDE);
	std::pair<std::string, std::string> mess;
	_pushMsgSync.lock();
	if (!_msgQueue.empty()) {
		_msgQueue.pop();
	}
	if (!_msgQueue.empty()) {
		mess = _msgQueue.front();
	}
	_pushMsgSync.unlock();
	if (!mess.second.empty()) {
		ShowMessage(mess.first, mess.second);
	}
}


void D5XMessageBox::OnCancel()
{
	// TODO: 在此添加专用代码和/或调用基类
	ConfirmOrCloseShowMessage();
	//CDialogEx::OnCancel();
}


void D5XMessageBox::OnOK()
{
	// TODO: 在此添加专用代码和/或调用基类
	ConfirmOrCloseShowMessage();
	//CDialogEx::OnOK();
}
